﻿using System;
using System.IO;
using System.Linq;
using System.Xml;
using YArchitech.LIB;

namespace HYCreateDuct.MethodHelp
{
	public class DuctHisHelp
	{
		public DuctHisHelp()
		{
			this.xmlPath = Path.Combine(Product.UserDataLocation, "HYAirSystemSetting.xml");
			if (!File.Exists(this.xmlPath))
			{
				File.Copy(Path.Combine(Product.DataLocation, "HYAirSystemSetting.xml"), this.xmlPath);
			}
			this.xmlDoc = XMLOperating.LoadXml(this.xmlPath);
			this.xmlNodeRoot = this.xmlDoc.FirstChild.NextSibling;
			if ((this.xmlNodeRoot as XmlElement).GetAttribute("version") != "BIMSpace2019")
			{
				File.Copy(Path.Combine(Product.DataLocation, "HYAirSystemSetting.xml"), this.xmlPath, true);
				this.xmlDoc = new XmlDocument();
				this.xmlDoc.Load(this.xmlPath);
				this.xmlNodeRoot = this.xmlDoc.FirstChild.NextSibling;
			}
		}

		public string GetValue(string parentNode, string subNode)
		{
			string result;
			try
			{
				result = this.xmlNodeRoot.SelectSingleNode(parentNode).SelectSingleNode(subNode).InnerText;
			}
			catch (Exception)
			{
				result = "";
			}
			return result;
		}

		public void WriteValue(string parentNode, string subNode, string text)
		{
			try
			{
				XmlNode xmlNode = this.xmlNodeRoot.SelectSingleNode(parentNode);
				if (xmlNode == null)
				{
					xmlNode = this.xmlDoc.CreateElement(parentNode);
					this.xmlNodeRoot.AppendChild(xmlNode);
				}
				XmlNode xmlNode2 = xmlNode.SelectSingleNode(subNode);
				if (xmlNode2 != null)
				{
					xmlNode2.InnerText = text;
				}
				else
				{
					XmlNode xmlNode3 = this.xmlDoc.CreateElement(subNode);
					xmlNode3.InnerText = text;
					xmlNode.AppendChild(xmlNode3);
				}
				this.xmlDoc.Save(this.xmlPath);
			}
			catch (Exception)
			{
			}
		}

		public DuctHis GetHistory()
		{
			DuctHis ductHis = new DuctHis();
			try
			{
				ductHis.nSystemType = int.Parse(this.GetValue("DuctSystemType", "nSystemType"));
				ductHis.nDuctType = int.Parse(this.GetValue("DuctSystemType", "nDuctType"));
				ductHis.nWindL = int.Parse(this.GetValue("DuctSystemType", "nWindL"));
				ductHis.nWidth = int.Parse(this.GetValue("DuctSystemType", "nWidth"));
				ductHis.nHeight = int.Parse(this.GetValue("DuctSystemType", "nHeight"));
				ductHis.nLevel = int.Parse(this.GetValue("DuctSystemType", "nLevel"));
				ductHis.nOffset = int.Parse(this.GetValue("DuctSystemType", "nOffset"));
				ductHis.nType = int.Parse(this.GetValue("DuctSystemType", "nType"));
				ductHis.nTypeOffset = int.Parse(this.GetValue("DuctSystemType", "nTypeOffset"));
				ductHis.xdLevel = int.Parse(this.GetValue("DuctSystemType", "xdLevel"));
				ductHis.nLevelS = int.Parse(this.GetValue("DuctSystemType", "nLevelS"));
				ductHis.nLevelSOffset = int.Parse(this.GetValue("DuctSystemType", "nLevelSOffset"));
				ductHis.nLevelE = int.Parse(this.GetValue("DuctSystemType", "nLevelE"));
				ductHis.nLevelEOffset = int.Parse(this.GetValue("DuctSystemType", "nLevelEOffset"));
				ductHis.nFlow = int.Parse(this.GetValue("DuctSystemType", "nFlow"));
				ductHis.nInner = int.Parse(this.GetValue("DuctSystemType", "nInner"));
				ductHis.nTypeSelect = int.Parse(this.GetValue("DuctSystemType", "nTypeSelect"));
				if (ductHis.nFlow <= 1)
				{
					ductHis.nFlow = 1000;
				}
			}
			catch
			{
				ductHis.nSystemType = 0;
				ductHis.nDuctType = 0;
				ductHis.nWindL = 500;
				ductHis.nWidth = 500;
				ductHis.nHeight = 500;
				ductHis.nLevel = 0;
				ductHis.nOffset = 0;
				ductHis.nType = 5;
				ductHis.nTypeOffset = 0;
				ductHis.xdLevel = 0;
				ductHis.nLevelS = 0;
				ductHis.nLevelSOffset = 0;
				ductHis.nLevelE = 0;
				ductHis.nLevelEOffset = 2800;
				ductHis.nFlow = 1000;
				ductHis.nInner = 0;
				ductHis.nTypeSelect = 0;
			}
			return ductHis;
		}

		public void WriteHistory(DuctHis his)
		{
			this.WriteValue("DuctSystemType", "nSystemType", his.nSystemType.ToString());
			this.WriteValue("DuctSystemType", "nDuctType", his.nDuctType.ToString());
			this.WriteValue("DuctSystemType", "nWindL", his.nWindL.ToString());
			this.WriteValue("DuctSystemType", "nWidth", his.nWidth.ToString());
			this.WriteValue("DuctSystemType", "nHeight", his.nHeight.ToString());
			this.WriteValue("DuctSystemType", "nLevel", his.nLevel.ToString());
			this.WriteValue("DuctSystemType", "nOffset", his.nOffset.ToString());
			this.WriteValue("DuctSystemType", "nType", his.nType.ToString());
			this.WriteValue("DuctSystemType", "nTypeOffset", his.nTypeOffset.ToString());
			this.WriteValue("DuctSystemType", "xdLevel", his.xdLevel.ToString());
			this.WriteValue("DuctSystemType", "nLevelS", his.nLevelS.ToString());
			this.WriteValue("DuctSystemType", "nLevelSOffset", his.nLevelSOffset.ToString());
			this.WriteValue("DuctSystemType", "nLevelE", his.nLevelE.ToString());
			this.WriteValue("DuctSystemType", "nLevelEOffset", his.nLevelEOffset.ToString());
			this.WriteValue("DuctSystemType", "nFlow", his.nFlow.ToString());
			this.WriteValue("DuctSystemType", "nInner", his.nInner.ToString());
			this.WriteValue("DuctSystemType", "nTypeSelect", his.nTypeSelect.ToString());
		}

		public void WriteHistory(int nInner, int nTypeSelect)
		{
			this.WriteValue("DuctSystemType", "nInner", nInner.ToString());
			this.WriteValue("DuctSystemType", "nTypeSelect", nTypeSelect.ToString());
		}

		public string[] GetFormDuctSetText()
		{
			string[] array = new string[]
			{
				"",
				"",
				""
			};
			array[0] = XMLOperating.GetNodeText(this.xmlDoc, "FormDuctSet", "cbPress");
			array[1] = XMLOperating.GetNodeText(this.xmlDoc, "FormDuctSet", "cbTemp");
			array[2] = XMLOperating.GetNodeText(this.xmlDoc, "FormDuctSet", "cbHumidity");
			return array;
		}

		public void WriteFormDuctSetText(string[] texts)
		{
			if (texts.Count<string>() >= 3)
			{
				this.WriteValue("FormDuctSet", "cbPress", texts[0]);
				this.WriteValue("FormDuctSet", "cbTemp", texts[1]);
				this.WriteValue("FormDuctSet", "cbHumidity", texts[2]);
			}
		}

		private XmlDocument xmlDoc;

		private string xmlPath = "";

		private XmlNode xmlNodeRoot;
	}
}
